package com.qdlc.p2p.dal.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.AdditionalFlowRepayPlanDao;
import com.qdlc.p2p.dal.dto.AdditionalFlowRepayPlan;

/**
 * 项目待还计划DAO接口
 * @author wjx
 * @version 2.0
 * @since 2015年05月15日 11时43分26秒
 */
@Repository(value = "additionalFlowRepayPlanDao")
public class AdditionalFlowRepayPlanDaoImpl extends BaseDaoImpl<AdditionalFlowRepayPlan> implements AdditionalFlowRepayPlanDao {

	private static Logger logger = Logger.getLogger(AdditionalFlowRepayPlanDaoImpl.class);
	
    @SuppressWarnings("unchecked")
    @Override
    public AdditionalFlowRepayPlan getAdditionalFlowRepayPlanById(long id) {
        String jpql = "from AdditionalFlowRepayPlan where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<AdditionalFlowRepayPlan> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (AdditionalFlowRepayPlan) list.get(0);
        } else {
            return null;
        }
    }

	@SuppressWarnings("rawtypes")
	@Override
	public int getAllWaitRepay(long projectId) {
		String jpql  ="select count(*) from AdditionalFlowRepayPlan where flowProject.id=?1 and webStatus = ?2";
		Query query = em.createQuery(jpql);
		query.setParameter(1, projectId);
		query.setParameter(2, (byte)0);
		List list = query.getResultList();
		if (list !=null && list.size() > 0) {
			return Integer.parseInt(list.get(0)+"");
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AdditionalFlowRepayPlan> allWaitRepayPlan() {
		//获取当天查询结束时间（由于还款时间精确到当天时分秒，所以查询时间以第二天起始时间为准）
		Date nextdate = DateUtil.rollDay(DateUtil.getNow(), 1);
		Date enddate =  DateUtil.getDayStartTime(nextdate.getTime()/1000);
		String jpql = " from AdditionalFlowRepayPlan where  repayTime <?1 and webStatus =0 and status =0";
		Query query = em.createQuery(jpql);
		query.setParameter(1, enddate);
		List<AdditionalFlowRepayPlan> list = query.getResultList();
		if (null !=list && list.size()>0) {
			return list;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AdditionalFlowRepayPlan> getRepayByPid(long pid) {
		String jpql = "from AdditionalFlowRepayPlan where flowProject.id = ?1";
		Query query = em.createQuery(jpql);
		query.setParameter(1, pid);
		List<AdditionalFlowRepayPlan> list = query.getResultList();
		return list;
	}

	@Override
	public void updateRepayPlan(AdditionalFlowRepayPlan plan) {
		
		String jpql = "update " + Global.DB_PREFIX + "additional_flow_repay_plan set web_status=?1 where id = ?2 and status =0";
		Query query = em.createNativeQuery(jpql);
		query.setParameter(1, (byte)1);
		query.setParameter(2, plan.getId());
		int result = query.executeUpdate();
		logger.info("流转标还款，更改状态影响条数："+result);
		plan = find(plan.getId());
		em.refresh(plan);
	}
    
    
    
}
